﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;

    public class Slider : LinearControl
    {
        private Color activeBarColor = Color.Red;
        private Color imageTransparentColor = Color.Transparent;
        private Color inactiveBarColor = Color.DarkBlue;
        private Color ledColor = Color.LightGreen;
        private Color sliderBarColor = Color.Gray;
        private Image sliderImage;
        private Size sliderSize = new Size(10, 20);

        protected virtual void DrawActiveBar(Graphics g)
        {
            Vector location = new Vector(base.Direction, base.StartPoint[base.Direction], base.CenterLine - (base.BarWidth / 2));
            Vector size = new Vector(base.Direction, this.ToPoint(base.Value)[base.Direction] - base.StartPoint[base.Direction], base.BarWidth);
            VectorRectangle rectangle = new VectorRectangle(location, size);
            g.FillRectangle(new SolidBrush(this.ActiveBarColor), rectangle.PositiveRectangle().Round());
        }

        protected override void DrawDynamic(Graphics g)
        {
            this.DrawActiveBar(g);
            this.DrawSlider(g);
            base.DrawDynamic(g);
        }

        protected virtual void DrawInactiveBar(Graphics g)
        {
            Vector location = new Vector(base.Direction, base.StartPoint[base.Direction], base.CenterLine - (base.BarWidth / 2));
            Vector size = new Vector(base.Direction, base.EndPoint[base.Direction] - base.StartPoint[base.Direction], base.BarWidth);
            VectorRectangle rectangle = new VectorRectangle(location, size);
            g.FillRectangle(new SolidBrush(this.InactiveBarColor), rectangle.PositiveRectangle().Round());
        }

        protected virtual void DrawSlider(Graphics g)
        {
            if (this.SliderImage != null)
            {
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(this.imageTransparentColor, this.imageTransparentColor);
                g.DrawImage(this.SliderImage, new Rectangle(((int) (this.SliderRectangle.X + (this.SliderRectangle.Width / 2M))) - (this.SliderImage.Size.Width / 2), ((int) (this.SliderRectangle.Y + (this.SliderRectangle.Height / 2M))) - (this.SliderImage.Size.Height / 2), this.SliderImage.Width, this.SliderImage.Height), 0, 0, this.SliderImage.Width, this.SliderImage.Height, GraphicsUnit.Pixel, imageAttr);
            }
            else
            {
                Rectangle rect = new Rectangle((int) this.SliderRectangle.X, (int) this.SliderRectangle.Y, (int) this.SliderRectangle.Width, (int) this.SliderRectangle.Height);
                g.FillRectangle(new SolidBrush(this.SliderBarColor), rect);
                rect.Inflate(-3, -3);
                g.FillRectangle(new SolidBrush(this.LedColor), rect);
            }
        }

        protected override void DrawStatic(Graphics g)
        {
            this.DrawInactiveBar(g);
            base.DrawStatic(g);
        }

        public Color ActiveBarColor
        {
            get
            {
                return this.activeBarColor;
            }
            set
            {
                if (value != this.activeBarColor)
                {
                    this.activeBarColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        protected override Size DefaultControlSize
        {
            get
            {
                return new Size(200, 100);
            }
        }

        public Color ImageTransparentColor
        {
            get
            {
                return this.imageTransparentColor;
            }
            set
            {
                if (this.imageTransparentColor != value)
                {
                    this.imageTransparentColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color InactiveBarColor
        {
            get
            {
                return this.inactiveBarColor;
            }
            set
            {
                if (value != this.inactiveBarColor)
                {
                    this.inactiveBarColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Color LedColor
        {
            get
            {
                return this.ledColor;
            }
            set
            {
                if (value != this.ledColor)
                {
                    this.ledColor = value;
                    base.Invalidate();
                }
            }
        }

        public Color SliderBarColor
        {
            get
            {
                return this.sliderBarColor;
            }
            set
            {
                if (value != this.sliderBarColor)
                {
                    this.sliderBarColor = value;
                    base.InvalidateStatic();
                }
            }
        }

        public Image SliderImage
        {
            get
            {
                return this.sliderImage;
            }
            set
            {
                if (value != this.sliderImage)
                {
                    this.sliderImage = value;
                    base.Invalidate();
                }
            }
        }

        private VectorRectangle SliderRectangle
        {
            get
            {
                Vector vector = this.ToPoint(base.Value);
                return new VectorRectangle(vector.X - (this.SliderSize.Width / 2), vector.Y - (this.SliderSize.Height / 2), this.SliderSize.Width, this.SliderSize.Height);
            }
        }

        public Size SliderSize
        {
            get
            {
                return this.sliderSize;
            }
            set
            {
                if (this.sliderSize != value)
                {
                    this.sliderSize = value;
                    base.InvalidateStatic();
                }
            }
        }
    }
}

